window._JGSHOP = {

    _CURRENTLABEL:null,

    npcForJg:null,

    npcRace:null,

    _PAGE:0,

    _PAGESIZE:9,

    _MAXPAGE:0,

    getNpcForJg:function(){
        if(_JGSHOP.npcForJg==null){
            _JGSHOP.npcForJg = [];
            for(var key in _NPC){

                var npcconfig = _NPC[key];
                if(!npcconfig.isboss
                    &&_GAMEDATAHELPER.hasNpcTj(key)
                    &&npcconfig.race==_JGSHOP.npcRace
                ){
                    _JGSHOP.npcForJg.push({value:key});
                }
            }

            if(_JGSHOP.npcForJg&&_JGSHOP.npcForJg.length>_JGSHOP._PAGESIZE){
                _JGSHOP._MAXPAGE = (_JGSHOP.npcForJg.length%_JGSHOP._PAGESIZE==0)?
                                    (_JGSHOP.npcForJg.length/_JGSHOP._PAGESIZE-1):
                                    Math.floor(_JGSHOP.npcForJg.length/_JGSHOP._PAGESIZE);
            }else{
                _JGSHOP._MAXPAGE = 0;
            }

        }

        return _JGSHOP.npcForJg;
    },

    showJG:function(parentNode,race){

        var uiObj = _GRM.getNodeObj('jiuguanshop');
        var mainLabel = uiObj.node;

        _JGSHOP._CURRENTLABEL = mainLabel;

        _JGSHOP.npcForJg = null;

        _JGSHOP._PAGE = 0;

        _JGSHOP.npcRace = race;

        _JGSHOP.getNpcForJg();
        
        if(uiObj.isnew){
            _JGSHOP.initButton(mainLabel);
        }

       
        _JGSHOP.refresh();

        parentNode.addChild(mainLabel);
    },


    initButton:function(mainLabel){

        var temMainNode = mainLabel.getChildByName('mainLabel');
        var btnInfoNode = temMainNode.getChildByName('btnInfo');
        var pageNode = temMainNode.getChildByName('page');
        var btnRight = pageNode.getChildByName('btnRight');
        var btnLeft = pageNode.getChildByName('btnLeft');
        

        mainLabel.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
        }, mainLabel);

        btnInfoNode.on(cc.Node.EventType.TOUCH_START, function(){
            _TIPS.showMessage('图鉴中点亮的非boss角色会出现在此商店中,每招募一个角色需要消耗招募券一张');
        }, mainLabel);

        var goodsArr = temMainNode.getChildByName('goods').getChildren();
        for(var i=0;i<goodsArr.length;i++){

            var goodsNode = goodsArr[i];
            var btnInfo = goodsNode.getChildByName('btnInfo');
            var btnBuy = goodsNode.getChildByName('btnbuy');
            goodsNode.goodsIndex = i;
            
            btnInfo.on(cc.Node.EventType.TOUCH_START, function(){
                //_TIPS.showMessage('详情:'+_JGSHOP.getRealGoodsIndex(this.goodsIndex));
                var realindex = _JGSHOP.getRealGoodsIndex(this.goodsIndex);
                _NPCDL.initUi(_MAP.node,{sence:_STYPE._FIGHTSENCE,
                    roleArr:_JGSHOP.getNpcForJg(),
                    roleIndex:realindex
                });

            }, goodsNode);

            btnBuy.on(cc.Node.EventType.TOUCH_START, function(){
                //_TIPS.showMessage('购买:'+_JGSHOP.getRealGoodsIndex(this.goodsIndex));
                var realindex = _JGSHOP.getRealGoodsIndex(this.goodsIndex);
                var npctype = _JGSHOP.getNpcForJg()[realindex].value;
                _MANAGERNMB.showNmbUi(_MAP.node,{goodsInfo:npctype,type:_MANAGERNMB._TYPEBUYPEOPLE,tipsnmb:1});
            }, goodsNode);

        }

        btnRight.on(cc.Node.EventType.TOUCH_START, function(){
            console.log('right');
            _JGSHOP._PAGE++;
            _JGSHOP.refresh();
        }, btnRight);

        btnLeft.on(cc.Node.EventType.TOUCH_START, function(){
            console.log('btnLeft');
            if(_JGSHOP._PAGE>0){
                _JGSHOP._PAGE--;
                _JGSHOP.refresh();
            }
        }, btnLeft);
    },

    refresh:function(){
        //var npcConfig = _NPC[_MSHOP._CURRENTNPCTYPE];
        var goodsArr = _JGSHOP.getNpcForJg();
        var mainLabel = _JGSHOP._CURRENTLABEL;
        var goodsNodes = mainLabel.getChildByName('mainLabel').getChildByName('goods').getChildren();
        var faceNode = mainLabel.getChildByName('mainLabel').getChildByName('face');
        var shopDescNode = faceNode.getChildByName('des');
        var faceImgNode = faceNode.getComponent(cc.Sprite);
        var pageNode = mainLabel.getChildByName('mainLabel').getChildByName('page');
        var pageNodeLabel =  pageNode.getChildByName('label');

        faceImgNode.spriteFrame = _GRM.getFaceFrame("问号");
        shopDescNode.getComponent(cc.Label).string = _RACE[_JGSHOP.npcRace].desc
                                                     +"商店";

        if(_JGSHOP._MAXPAGE>0){
            pageNode.active = true;
            pageNodeLabel.getComponent(cc.Label).string = "第"+(_JGSHOP._PAGE+1)+"页";
            if(_JGSHOP._PAGE>0){
                pageNode.getChildByName('btnLeft').active = true;
            }else{
                pageNode.getChildByName('btnLeft').active = false;
            }

            if(_JGSHOP._PAGE<_JGSHOP._MAXPAGE){
                pageNode.getChildByName('btnRight').active = true;
            }else{
                pageNode.getChildByName('btnRight').active = false;
            }
        }else{
            pageNode.active = false;
        }

        for(var i=0;i<goodsNodes.length;i++){
            var realindex = _JGSHOP.getRealGoodsIndex(i);
            var goodsnode = goodsNodes[i];
            if(goodsArr[realindex]){
                goodsnode.active = true;
                _JGSHOP.refreshOne(goodsnode.getChildByName('imgs'),goodsArr[realindex].value);
            }else{
                goodsnode.active = false;
            }
        }
    },

    refreshOne:function(node,npctype){
        var npctypeConfig = _NPC[npctype];
        if(npctypeConfig){

            var npcjob = npctypeConfig.job;
            var npcrace = npctypeConfig.race;
            var npcqua = npctypeConfig.level;

            node.active = true;
            node.roleName = npctype;

            var faceNode = node.getChildByName('img');
            var nameNode = node.getChildByName('name');
            var ftypeNode = node.getChildByName('wuxin');
            var ftypeStrNode = ftypeNode.getChildByName('label');

            var jobNode = node.getChildByName('job');
            var jobStrNode = jobNode.getChildByName('ftypeStr');
            jobNode.color = new cc.color(window._JOB[npcjob].color);
            jobStrNode.getComponent(cc.Label).string = window._JOB[npcjob].sname;
            var zhongNode = node.getChildByName('zhong');
            var zhongStrNode = zhongNode.getChildByName('ftypeStr');
            zhongNode.color = new cc.color(window._RACE[npcrace].color);
            zhongStrNode.getComponent(cc.Label).string = window._RACE[npcrace].name;
            var quaNode = node.getChildByName('level');
            var quaStrNode = quaNode.getChildByName('ftypeStr');
            quaNode.color = new cc.color(window._CS.qua[npcqua].color);
            quaStrNode.getComponent(cc.Label).string = npcqua;

            faceNode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(npctypeConfig.face);
            nameNode.getComponent(cc.Label).string = npctype;
            ftypeNode.color = new cc.color(_CS.ftype[npctypeConfig.ftype].color);
            ftypeStrNode.getComponent(cc.Label).string = npctypeConfig.ftype;


        }else{

            node.active = false;

        }
        
    },

    getRealGoodsIndex:function(i){

        return _JGSHOP._PAGE*_JGSHOP._PAGESIZE+i;
    },

    getPrice:function(){
        return 300000;
    }

}